Trade Management


In [1]:
import pandas as pd
import oandapyV20
import oandapyV20.endpoints.trades as trades 
import configparser

In [2]:
config = configparser.ConfigParser()
config.read('../config/config_v20.ini')
accountID = config['oanda']['account_id']
access_token = config['oanda']['api_key']

Get a list of trades for an Account.


In [3]:
client = oandapyV20.API(access_token=access_token)

In [4]:
params ={
          "instrument": "DE30_EUR,EUR_USD"
        }

In [5]:
r = trades.TradesList(accountID=accountID, params=params)

In [6]:
client.request(r)


Out[6]:
{'lastTransactionID': '63',
 'trades': [{'currentUnits': '100',
   'financing': '0.0090',
   'id': '35',
   'initialUnits': '100',
   'instrument': 'NZD_USD',
   'openTime': '2017-01-20T15:59:08.362429413Z',
   'price': '0.71532',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '1.4736'},
  {'currentUnits': '100',
   'financing': '0.0078',
   'id': '33',
   'initialUnits': '100',
   'instrument': 'AUD_USD',
   'openTime': '2017-01-20T15:58:58.618457963Z',
   'price': '0.75473',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '-0.0458'},
  {'currentUnits': '100',
   'financing': '0.0078',
   'id': '31',
   'initialUnits': '100',
   'instrument': 'AUD_USD',
   'openTime': '2017-01-20T15:58:23.903964257Z',
   'price': '0.75489',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '-0.0688'}]}

In [7]:
print(r.response)


{'lastTransactionID': '63', 'trades': [{'state': 'OPEN', 'currentUnits': '100', 'realizedPL': '0.0000', 'price': '0.71532', 'openTime': '2017-01-20T15:59:08.362429413Z', 'unrealizedPL': '1.4736', 'initialUnits': '100', 'instrument': 'NZD_USD', 'id': '35', 'financing': '0.0090'}, {'state': 'OPEN', 'currentUnits': '100', 'realizedPL': '0.0000', 'price': '0.75473', 'openTime': '2017-01-20T15:58:58.618457963Z', 'unrealizedPL': '-0.0458', 'initialUnits': '100', 'instrument': 'AUD_USD', 'id': '33', 'financing': '0.0078'}, {'state': 'OPEN', 'currentUnits': '100', 'realizedPL': '0.0000', 'price': '0.75489', 'openTime': '2017-01-20T15:58:23.903964257Z', 'unrealizedPL': '-0.0688', 'initialUnits': '100', 'instrument': 'AUD_USD', 'id': '31', 'financing': '0.0078'}]}

Get the list of open Trades for an Account.


In [8]:
r = trades.OpenTrades(accountID)

In [9]:
client.request(r)


Out[9]:
{'lastTransactionID': '63',
 'trades': [{'currentUnits': '100',
   'financing': '0.0090',
   'id': '35',
   'initialUnits': '100',
   'instrument': 'NZD_USD',
   'openTime': '2017-01-20T15:59:08.362429413Z',
   'price': '0.71532',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '1.4736'},
  {'currentUnits': '100',
   'financing': '0.0078',
   'id': '33',
   'initialUnits': '100',
   'instrument': 'AUD_USD',
   'openTime': '2017-01-20T15:58:58.618457963Z',
   'price': '0.75473',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '-0.0458'},
  {'currentUnits': '100',
   'financing': '0.0078',
   'id': '31',
   'initialUnits': '100',
   'instrument': 'AUD_USD',
   'openTime': '2017-01-20T15:58:23.903964257Z',
   'price': '0.75489',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '-0.0688'}]}

In [10]:
r.response


Out[10]:
{'lastTransactionID': '63',
 'trades': [{'currentUnits': '100',
   'financing': '0.0090',
   'id': '35',
   'initialUnits': '100',
   'instrument': 'NZD_USD',
   'openTime': '2017-01-20T15:59:08.362429413Z',
   'price': '0.71532',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '1.4736'},
  {'currentUnits': '100',
   'financing': '0.0078',
   'id': '33',
   'initialUnits': '100',
   'instrument': 'AUD_USD',
   'openTime': '2017-01-20T15:58:58.618457963Z',
   'price': '0.75473',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '-0.0458'},
  {'currentUnits': '100',
   'financing': '0.0078',
   'id': '31',
   'initialUnits': '100',
   'instrument': 'AUD_USD',
   'openTime': '2017-01-20T15:58:23.903964257Z',
   'price': '0.75489',
   'realizedPL': '0.0000',
   'state': 'OPEN',
   'unrealizedPL': '-0.0688'}]}

In [11]:
trade_id = r.response['trades'][0]['id']

Get the details of a specific Trade in an Account.


In [12]:
r = trades.TradeDetails(accountID, tradeID=trade_id)

In [13]:
client.request(r)


Out[13]:
{'lastTransactionID': '63',
 'trade': {'currentUnits': '100',
  'financing': '0.0090',
  'id': '35',
  'initialUnits': '100',
  'instrument': 'NZD_USD',
  'openTime': '2017-01-20T15:59:08.362429413Z',
  'price': '0.71532',
  'realizedPL': '0.0000',
  'state': 'OPEN',
  'unrealizedPL': '1.4736'}}

In [14]:
print(r.response)


{'lastTransactionID': '63', 'trade': {'state': 'OPEN', 'currentUnits': '100', 'realizedPL': '0.0000', 'price': '0.71532', 'openTime': '2017-01-20T15:59:08.362429413Z', 'unrealizedPL': '1.4736', 'initialUnits': '100', 'instrument': 'NZD_USD', 'id': '35', 'financing': '0.0090'}}

Close (partially or fully) a specific open Trade in an Account.


In [15]:
data = {
          "units": 100
       }

In [16]:
r = trades.TradeClose(accountID, tradeID=trade_id)

In [17]:
client.request(r)


Out[17]:
{'lastTransactionID': '65',
 'orderCancelTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '64',
  'id': '65',
  'orderID': '64',
  'reason': 'MARKET_HALTED',
  'time': '2017-01-28T13:31:38.732264064Z',
  'type': 'ORDER_CANCEL',
  'userID': 5120068},
 'orderCreateTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '64',
  'id': '64',
  'instrument': 'NZD_USD',
  'positionFill': 'REDUCE_ONLY',
  'reason': 'TRADE_CLOSE',
  'time': '2017-01-28T13:31:38.732264064Z',
  'timeInForce': 'FOK',
  'tradeClose': {'tradeID': '35', 'units': 'ALL'},
  'type': 'MARKET_ORDER',
  'units': '-100',
  'userID': 5120068},
 'relatedTransactionIDs': ['64', '65']}